package com.dianping.cat.home.heartbeat.transform;

import com.dianping.cat.home.heartbeat.IEntity;
import com.dianping.cat.home.heartbeat.IVisitor;
import com.dianping.cat.home.heartbeat.entity.Group;
import com.dianping.cat.home.heartbeat.entity.HeartbeatDisplayPolicy;
import com.dianping.cat.home.heartbeat.entity.Metric;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;
  
/*  34 */   public DefaultXmlBuilder() { this(false); }



/*  38 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }
  
  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  42 */     this.m_compact = compact;
/*  43 */     this.m_sb = sb;
/*  44 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }
  
  public String buildXml(IEntity<?> entity) {
/*  48 */     entity.accept(this.m_visitor);
/*  49 */     return this.m_sb.toString();
  }
  
  protected void endTag(String name) {
/*  53 */     this.m_level--;

/*  55 */     indent();
/*  56 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  
/*  60 */   protected String escape(Object value) { return escape(value, false); }

  
  protected String escape(Object value, boolean text) {
/*  64 */     if (value == null) {
/*  65 */       return null;
    }
    
/*  68 */     String str = toString(value);
/*  69 */     int len = str.length();
/*  70 */     StringBuilder sb = new StringBuilder(len + 16);

/*  72 */     for (int i = 0; i < len; i++) {
/*  73 */       char ch = str.charAt(i);

/*  75 */       switch (ch) {
        case '<':
/*  77 */           sb.append("&lt;");
          break;
        case '>':
/*  80 */           sb.append("&gt;");
          break;
        case '&':
/*  83 */           sb.append("&amp;");
          break;
        case '"':
/*  86 */           if (!text) {
/*  87 */             sb.append("&quot;");
            break;
          }
        default:
/*  91 */           sb.append(ch);
          break;
      }
    
    }
/*  96 */     return sb.toString();
  }
  
  protected void indent() {
/* 100 */     if (!this.m_compact) {
/* 101 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 102 */         this.m_sb.append("   ");
      }
    }
  }

  
/* 108 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 112 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 116 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }

  
  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 120 */     indent();

/* 122 */     this.m_sb.append('<').append(name);

/* 124 */     int len = nameValues.length;

/* 126 */     for (int i = 0; i + 1 < len; i += 2) {
/* 127 */       Object attrName = nameValues[i];
/* 128 */       Object attrValue = nameValues[i + 1];

/* 130 */       if (attrValue != null) {
/* 131 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }
    
/* 135 */     if (dynamicAttributes != null) {
/* 136 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 137 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }
    
/* 141 */     if (text != null && closed) {
/* 142 */       this.m_sb.append('>');
/* 143 */       this.m_sb.append(escape(text, true));
/* 144 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 146 */       if (closed) {
/* 147 */         this.m_sb.append('/');
      } else {
/* 149 */         this.m_level++;
      }
      
/* 152 */       this.m_sb.append(">\r\n");
    }
  }

  
  protected String toString(Object value) {
/* 158 */     if (value instanceof String)
/* 159 */       return (String)value; 
/* 160 */     if (value instanceof Collection) {
/* 161 */       Collection<Object> list = (Collection)value;
/* 162 */       StringBuilder sb = new StringBuilder(32);
/* 163 */       boolean first = true;

/* 165 */       for (Object item : list) {
/* 166 */         if (first) {
/* 167 */           first = false;
        } else {
/* 169 */           sb.append(',');
        }
        
/* 172 */         if (item != null) {
/* 173 */           sb.append(item);
        }
      }
      
/* 177 */       return sb.toString();
/* 178 */     }  if (value.getClass().isArray()) {
/* 179 */       int len = Array.getLength(value);
/* 180 */       StringBuilder sb = new StringBuilder(32);
/* 181 */       boolean first = true;

/* 183 */       for (int i = 0; i < len; i++) {
/* 184 */         Object item = Array.get(value, i);

/* 186 */         if (first) {
/* 187 */           first = false;
        } else {
/* 189 */           sb.append(',');
        }
        
/* 192 */         if (item != null) {
/* 193 */           sb.append(item);
        }
      }
      
/* 197 */       return sb.toString();
    }
    
/* 200 */     return String.valueOf(value);
  }

  
  public void visitGroup(Group group) {
/* 205 */     startTag("group", null, new Object[] { "id", group.getId(), "order", Integer.valueOf(group.getOrder()) });

/* 207 */     if (!group.getMetrics().isEmpty()) {
/* 208 */       for (Metric metric : group.getMetrics().values()) {
/* 209 */         metric.accept(this.m_visitor);
      }
    }
    
/* 213 */     endTag("group");
  }

  
  public void visitHeartbeatDisplayPolicy(HeartbeatDisplayPolicy heartbeatDisplayPolicy) {
/* 218 */     startTag("heartbeat-display-policy", null, new Object[0]);

/* 220 */     if (!heartbeatDisplayPolicy.getGroups().isEmpty()) {
/* 221 */       for (Group group : heartbeatDisplayPolicy.getGroups().values()) {
/* 222 */         group.accept(this.m_visitor);
      }
    }
    
/* 226 */     endTag("heartbeat-display-policy");
  }


  
/* 231 */   public void visitMetric(Metric metric) { startTag("metric", true, null, new Object[] { "id", metric.getId(), "unit", metric.getUnit(), "delta", Boolean.valueOf(metric.isDelta()), "order", Integer.valueOf(metric.getOrder()), "title", metric.getTitle(), "lable", metric.getLable(), "alert", Boolean.valueOf(metric.isAlert()) }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/heartbeat/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */